/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission 
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License 
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably 
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

package com.funambol.util;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * A class useful to see the content of Log RecordStore generated by RMSAppender
 *
 */
public class LogViewer {
    
    //---------------------------------------------------------------- Constants
    /**
     * Pass this value to <code>getLogEntries(int mode)</code> method
     * for RMS Log Mode
     */
    public static final int RMSLOG = 0;
    /**
     * Pass this value to <code>getLogEntries(int mode)</code> method
     * for File Log Mode
     */
    public static final int JSR75LOG = 1;
    
    //------------------------------------------------------------- Constructors
    /**
     * Creates a new instance of LogViewer
     */
    public LogViewer() {
    }
    
    //----------------------------------------------------------- Public Methods
    /**
     * This method return a String array that contain the log file content
     * in form of String objects. For now it just can access RMS Log file,
     * but empty method for accessing JSR75 Log file is provided.
     *
     * @return a String array that contain the specified log
     */
    public String[] getLogEntries(int mode) {
        String[] logArray = null;
        if (mode == RMSLOG) {
            logArray = getRMSLogStrings();
        } else if (mode == JSR75LOG) {
            Log.info("JSR75 will be supported in future versions");
        }
        return logArray;
    }
    
    //-----------------------------------------------------------Private Methods
    /**
     * Recursively scan the Log RecordStore RMSAppender.LOGDBNAME
     * and store its content into a String array of String objects.
     *
     * @return result String array that contains all log messages
     */
    private String[] getRMSLogStrings() {
        RecordStore rs = null;
        String[] result = null;
        try {
            rs = RecordStore.openRecordStore(RMSAppender.LOGDBNAME, true);
            int size = rs.getNumRecords();
            if (size == 0) {
                result = new String[1];
                result[0] = "No Log entries found.";
            } else {
                result = recordsToStringArray(rs);
            }
            rs.closeRecordStore();
        } catch (RecordStoreException ex) {
            Log.error(this, "getRMSLogStrings() recordstore Exception");
            ex.printStackTrace();
        }
        return result;
    }
    
    private String[] recordsToStringArray(RecordStore rs) throws RecordStoreNotOpenException {
        int i=0;
        String[] result = new String[rs.getNumRecords()];
        RecordEnumeration re = rs.enumerateRecords(null, null, false);
        while (re.hasNextElement()) {
            try {
                result [i++] = new String(re.nextRecord());
            } catch (Throwable t) {
                Log.error("throwable catched at recordsStoStringArray:  "+ t.toString());
                t.printStackTrace();
            }
        }
        return result;
    }
    
    /**
     * JSR75 Log File viewer. It will be implemented in future versions.
     */
    private void getJsr75LogFile() {
    }
}
